﻿export default {
  props: ['dialogInfo'],
  data () {
    return {
      dialogTitle: '维度表达式',
      dialogFormVisible: false,
      keyStr: '',
      keyName: '',
      keyExpress: '',
      expressList: [],
      columnsList: []
    }
  },
  created () {
    this.initDialog()
  },
  methods: {
    // 初始化表单时
    initDialog () {
      this.dialogTitle = this.dialogInfo.type === 'dimensionArray' ? '维度表达式' : '量度表达式'
      this.keyStr = this.dialogInfo.keyStr
      this.keyName = this.dialogInfo.keyName
      this.keyExpress = this.dialogInfo.keyExpress
      this.columnsList = this.dialogInfo.columnsList
      // 获取表达式
      this.getAllExpressList()
    },
    // 获取表达式
    getAllExpressList () {
      this.$get('/data_set/bi_function/list').then(data => {
        let dataResult = data.data.result
        if (dataResult.code === 'success') {
          this.expressList = dataResult.data
        } else {
          this.$message({
            type: 'error',
            message: dataResult.msg
          })
        }
      }).catch(error => {
        console.log(error)
      })
    },
    // 点击字段或者是引用函数时
    addExpress (val) {
      let dimensionE = document.getElementById('expressContent')
      if (dimensionE.selectionStart === undefined || dimensionE.selectionEnd === undefined) {
        return
      }
      let dimensionStr = this.keyExpress
      if (dimensionStr) {
        let startStr = dimensionStr.slice(0, dimensionE.selectionStart)
        let endStr = dimensionStr.slice(dimensionE.selectionEnd)
        this.keyExpress = startStr + val + endStr
      } else {
        this.keyExpress = val
      }
      dimensionE.focus()
    },
    // 点击弹窗的取消按钮
    resetForm () {
      this.$emit('operationReset')
    },
    closeDialog () {
      this.$emit('operationReset')
    },
    // 点击弹窗的确定按钮
    submitForm () {
      let toParent = {
        keyStr: this.keyStr,
        keyName: this.keyName,
        keyExpress: this.keyExpress,
        index: this.dialogInfo.index,
        type: this.dialogInfo.type
      }
      this.$emit('operationSuccess', toParent)
    }
  },
  watch: {
    'dialogInfo.dialogFormVisible': {
      handler: function (val, oldVal) {
        this.dialogFormVisible = val
        if (val) {
          this.initDialog()
        }
      },
      deep: true
    }
  }
}
